<template>
  <div style="background-color: #fff !important">
    <vab-query-form>
      <vab-query-form-top-panel v-if="showQuery">
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-form-item label="资源地址">
            <el-input
              v-model="tableOptions.queryForm.resourceUrl"
              placeholder="资源地址"
            />
          </el-form-item>
          <el-form-item label="资源名称">
            <el-input
              v-model="tableOptions.queryForm.resourceName"
              placeholder="资源名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
            >
              重置
            </el-button>
            <el-button icon="Search" type="primary" @click="queryTableData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="24">
        <el-button
          v-auth="['resource/xz']"
          icon="circle-plus"
          type="primary"
          @click="handleAdd"
        >
          添加
        </el-button>
        <el-button
          v-auth="['resource/sc']"
          icon="remove"
          type="danger"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="left"
        label="资源名称"
        prop="resourceName"
        width="180"
      />
      <el-table-column
        align="left"
        label="资源地址"
        prop="resourceUrl"
        width="340"
      />
      <el-table-column
        align="left"
        label="关联菜单"
        prop="menuFullName"
        show-overflow-tooltip
        width="220"
      />
      <el-table-column
        align="left"
        label="备注"
        prop="resourceRemark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="dateFormat"
        label="创建时间"
        prop="createTime"
      />
      <el-table-column align="center" fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            icon="Edit"
            v-auth="['resource/xg']"
            size="small"
            type="warning"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            icon="Remove"
            v-auth="['resource/sc']"
            size="small"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <ItemEdit ref="editRef" width="40%" @confirm="handleConfirmEdit" />
  </div>
</template>
<script setup name="ResourceMain">
import { dateFormat } from "@/utils/common";
import { queryTable, deleteObjs } from "@/api/admin/system/resource/";
import { useTable } from "@/hooks/table";
import { useMessage, useMessageBox } from "@/hooks/message";
import { getTableHeight } from "@/utils/util";
import ItemEdit from "./components/itemEdit.vue";

const props = defineProps({
  showQuery: {
    type: Boolean,
    default: true
  },
  autoLoad: {
    type: Boolean,
    default: false
  }
});

const editRef = ref(null);
const tableRef = ref(null);

const state = reactive({
  menu: {
    id: "",
    menuFullName: ""
  }
});

const tableOptions = reactive({
  autoLoad: props.autoLoad,
  queryForm: {
    menuId: "",
    resourceUrl: "",
    resourceName: ""
  },
  height: props.showQuery
    ? getTableHeight(1, true, true) - 10
    : getTableHeight(3, true, true) - 10,
  queryFunction: queryTable,
  descs: ["create_time"]
});

onMounted(async () => {
  console.log("show", tableOptions.height, props.showQuery);
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const handleAdd = () => {
  editRef.value.show({
    menuId: state.menu.id,
    menuFullName: state.menu.menuFullName
  });
};

const handleDelete = row => {
  let ids = null;
  if (row && row.id) {
    ids = row.id;
  } else {
    if (tableOptions.dateSelections.length > 0) {
      ids = tableOptions.dateSelections.map(item => item.id).join();
    } else {
      useMessage().error("未选中任何行");
      return;
    }
  }
  useMessageBox()
    .confirm("删除的资源将无法恢复，是否继续")
    .then(async () => {
      const { code, msg } = await deleteObjs(ids);
      if (code === 200) {
        useMessage().success(msg);
        queryTableData();
      } else {
        useMessage().error(msg);
      }
    });
};

const handleEdit = row => {
  if (row.id) {
    editRef.value.show(row, false);
  } else {
    useMessage().error("未选中任何行");
  }
};

const handleConfirmEdit = () => {
  queryTableData();
};

const show = async menu => {
  console.log("show", tableOptions.height, props.showQuery);
  if (menu) {
    if (menu.id) {
      state.menu.id = menu.id;
      tableOptions.queryForm.menuId = menu.id;
      tableOptions.dataList = [];
      await queryTableData();
    }
    if (menu.menuFullName) {
      state.menu.menuFullName = menu.menuFullName;
    }
  }
};

const getTotal = () => {
  return tableOptions.pagination.total;
};

defineExpose({
  show,
  getTotal
});
</script>
<style lang="scss" scoped></style>
